package com.yoso.mq.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.context.annotation.Bean;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author shizejin
 * @date 2021/10/20
 * @description:
 */
@Component
public class YosoRabbitListener {
    /*工作队列···*/
    @RabbitListener(queuesToDeclare = @Queue("workQueue"))
    public void listenWorkQueue1(String message) throws InterruptedException {
        System.out.println("监听者1监听到 = [" + message + "]");
        Thread.sleep(20);
    }
    @RabbitListener(queues = "workQueue")
    public void listenWorkQueue2(String message) throws InterruptedException {
        System.out.println("监听者2监听到 = [" + message + "]");
        Thread.sleep(200);

    }

    /*广播队列···*/
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("fanoutQueue1"),
            exchange = @Exchange(value = "fanoutExchange",type = ExchangeTypes.FANOUT)
    ))
    public void listenFanoutQueue1(String message){
        System.out.println("监听者1收到 = [" + message + "]");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("fanoutQueue2"),
            exchange = @Exchange(value = "fanoutExchange",type = ExchangeTypes.FANOUT)
    ))
    public void listenFanoutQueue2(String message){
        System.out.println("监听者2收到 = [" + message + "]");
    }

    /*direct 队列*/
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("directQueue1"),
            exchange = @Exchange(value = "directExchange",type = ExchangeTypes.DIRECT),
            key = {"yss","yoso"}
    ))
    public void listenDirectQueue1(String message){
        System.out.println("监听者1收到 = [" + message + "]");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("directQueue2"),
            exchange = @Exchange(value = "directExchange",type = ExchangeTypes.DIRECT),
            key = {"yss","tvb"}
    ))
    public void listenDirectQueue2(Object message){
        System.out.println("监听者2收到 = [" + message + "]");
    }

    /*topic队列···*/
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("topicQueue1"),
            exchange = @Exchange(value = "topicExchange",type = ExchangeTypes.TOPIC),
            key = {"tvb.news","yoso.#"}
    ))
    public void listenTopicExchange1(String message){
        System.out.println("监听者1收到 = [" + message + "]");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("topicQueue2"),
            exchange = @Exchange(value = "topicExchange",type = ExchangeTypes.TOPIC),
            key = {"tvb.old","yoso.old"}
    ))
    public void listenTopicExchange2(String message){
        System.out.println("监听者2收到 = [" + message + "]");
    }

//    delay 队列
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("delay"),
            exchange = @Exchange(value = "delay.exchange",delayed = "true"),
            key = {"delay"}
    ))
    public void listenDelayQueue1(String message){
        System.out.println("监听者收到 = [" + message + "]");
    }


    //    手动应答 队列
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("manualQueue"),
            exchange = @Exchange(value = "manualExchange",type = ExchangeTypes.FANOUT)
    ),ackMode = "MANUAL")
    public void listenManualQueue(String message, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Channel channel) throws IOException {
        try {
//            int i = 1/0;
//            RabbitMQ的ack机制中，第二个参数返回true，表示需要将这条消息投递给其他的消费者重新消费
            channel.basicAck(deliveryTag,false);
        } catch (IOException e) {
            // 第三个参数true，表示这个消息会重新进入队列
            channel.basicNack(deliveryTag, false, true);
            e.printStackTrace();
        }
        System.out.println("监听者收到 = [" + message + "]");
    }

}
